<?php

namespace app\common\validate\product;
use app\common\model\product\ShopProductTypeModel;
use think\Validate;
/**
 * 商品管理验证器
 */
class ShopProductValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'title' => 'require',
        'content' => 'require',
        'is_disable' => ['require', 'in' => '0,1'],
        'store_platform_id' => 'require',
        'shop_product_type_id' => 'require',
        'shop_product_type_code' => 'require',
        'merchant_shop_id' => 'require',
        'setting_refund_id' => 'require',
        'e_time_type' => ['require', 'in' => '0,1','checkTimeTypeExisted'],
        'product_goods' => ['require', 'array','checkProductGoodsExisted'],
        'enter_shop_types' => ['require', 'array','checkEnterShopTypesExisted'],
        'release_order_num'=> ['require','checkReleaseOrderNumExisted'],
        'price_details' => ['require', 'array'],
        'order_num'=>['require', 'gt:0'],
        'e_revoke_time'=>'require',
        'auth_status' => ['require', 'in' => '0,1,2'],
        'is_top' => ['require', 'in' => '0,1'],
    ];
    // 错误信息
    protected $message = [
        'store_platform_id.require' => '请选择平台',
        'shop_product_type_id.require' => '请选择活动类型',
        'shop_product_type_code.require' => '请选择活动类型',
        'merchant_shop_id.require' => '请选择店铺',
        'setting_refund_id.require' => '请选择返款类型',
        'e_time_type.require' => '请设置放单计划时间类型',
        'product_goods.require' => '请完善商品信息',
        'enter_shop_types.require' => '请添加进店方式',
        'price_details.require' => '费用明细有误，请稍后重试',
        'order_num.require' => '订单数量未获取到',
        'order_num.gt' => '总订单数不能小于1，请添加进店方式',
        'e_revoke_time.require' => '请设置自动撤销时间',
        'auth_status.require' => '请选择审核状态',
        'is_top.require' => '请选择是否置顶',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['store_platform_id','shop_product_type_id','shop_product_type_code','merchant_shop_id','setting_refund_id','e_time_type','product_goods','enter_shop_types','price_details','order_num','release_order_num','e_revoke_time'],
        'edit'    =>  ['id', 'store_platform_id','shop_product_type_id','shop_product_type_code','merchant_shop_id','setting_refund_id','e_time_type','product_goods','enter_shop_types','price_details','order_num','release_order_num','e_revoke_time'],
        'dele'    =>  ['ids'],
        'revoke'    =>  ['ids'],
        'pay'    =>  ['ids'],
        'halt'    =>  ['ids'],
        'open'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'auth' =>  ['ids', 'auth_status'],
        'is_top'=>  ['ids', 'is_top'],
    ];
    //验证商品信息
    protected function checkProductGoodsExisted($value, $rule, $data = [])
    {
        foreach ($data['product_goods'] as $key=>$val) {
            if(!$val['goods_url']){
                return "请输入商品链接";
            }
            if(!$val['goods_title']){
                return '请输入商品名称';
            }
            if(!$val['image_id']){
                return '请上传商品主图';
            }
            if($key<1 && !$val['show_price']){
                return '请输入展示价格';
            }
            if($data['shop_product_type_code']!=ShopProductTypeModel::LLRW  && !$val['order_price']){
                return '请输入下单价格';
            }
            if($data['shop_product_type_code']==ShopProductTypeModel::DFRW  && !$val['goods_num']){
                return '请输入下单件数';
            }
            if($data['shop_product_type_code']==ShopProductTypeModel::PTBT && !$val['activity_title']){
                return '请输入活动名称';
            }
            if($data['shop_product_type_code']==ShopProductTypeModel::PTBT  &&  !$val['activity_image_id']){
                return '请上传活动主图';
            }
            if($data['shop_product_type_code']==ShopProductTypeModel::PTBT  &&  !$val['activity_price']){
                return '请输入活动价格';
            }
            if($data['shop_product_type_code']==ShopProductTypeModel::PTBT && $val['activity_price']>$val['order_price']){
                return '活动价格不能大于下单价格';
            }
        }
        return true;
    }
    //效验进店方式
    protected function checkEnterShopTypesExisted($value, $rule, $data = [])
    {
        foreach ($data['enter_shop_types'] as $key=>$val) {
            if(!$val['order_num'] || $val['order_num']<1){
                return "请设置进店方式第".($key+1)."行的订单数量";
            }
            if($val['enter_type']==2 && !$val['qr_image_id']){
                return "请上传进店方式第".($key+1)."行的二维码";
            }
            if($val['enter_type']!=2 && !$val['keyword'] ){
                $key_title = $val['enter_type']==1?'口令':($val['enter_type']==3?'链接':'关键词');
                return "请输入进店方式第".($key+1)."行的".$key_title;
            }
            return true;
        }
    }
    //验证投放计划
    protected function checkTimeTypeExisted($value, $rule, $data = []){
        switch (intval($data['e_time_type'])){
            case 0:
                foreach ($data['creat_order_plan'] as $key=>$val) {
                    if(!$val['start_time']){
                        return '设置放单计划：请设置开始时间';
                    }
                    if(!$val['interval_minutes'] || $val['interval_minutes']<=0){
                        return '设置放单计划：请输入间隔分钟';
                    }
                    if(!$val['order_num'] || $val['order_num']<=0){
                        return '设置放单计划：请输入下单数量';
                    }
                    if(!$val['total_order_num'] || $val['total_order_num']<=0){
                        return '设置放单计划：请输入共放几单';
                    }
                }
                break;
            case 1:
                if(count($data['e_order_time']) !=2){
                    return '设置放单计划：请设置立刻放单时间范围';
                }
                break;
            default:
                return '设置放单计划：时间类型有误';
                break;
        }
        return true;
    }
    //验证放单总数
    protected function checkReleaseOrderNumExisted($value, $rule, $data = []){
        if(intval($data['e_time_type'])==0 && $data['release_order_num']!=$data['order_num']){
            return '设置放单计划订单数量错误';
        }
        return true;
    }

}
